const db = require("../../config/db");

module.exports = {
    add_order(order_id, order_name, order_price, order_img, order_color, order_size, order_num, order_phone, order_addr, order_user, order_status, order_create_time) {
        console.log("3.0", order_create_time);
        return new Promise((resolve, reject) => {
            let check_order_sql = `select order_id from db_order where order_id="${order_id}"`;
            db.query(check_order_sql, (err, result) => {
                // console.log("1.0===>", result);
                if (err) {
                    reject(err);
                } else {
                    if (result.length == 0) {
                        let add_order_sql = `insert into db_order(order_id,order_name,order_price,order_img,order_color,order_size,order_num,order_phone,order_addr,order_user,order_status,order_create_time) values("${order_id}","${order_name}","${order_price}","${order_img}","${order_color}","${order_size}","${order_num}","${order_phone}","${order_addr}","${order_user}",${order_status},"${order_create_time}")`;

                        db.query(add_order_sql, (err, result) => {
                            if (err) {
                                reject(err);
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "订单添加成功",
                                });
                            }
                        });
                    } else {
                        let updata_order_sql = `UPDATE db_order SET order_addr="${order_addr}",order_phone="${order_phone}",order_user="${order_user}",order_status="${order_status}" WHERE order_id="${order_id}"`;
                        db.query(updata_order_sql, (err, result) => {
                            if (err) {
                                reject(err);
                            } else {
                                resolve({
                                    code: 200,
                                });
                            }
                        });
                    }
                }
            });
        });
    },

    get_order(order_status) {
        return new Promise((resolve, reject) => {
            let get_order_sql = `select * from db_order where order_status="${order_status}"`;
            db.query(get_order_sql, (err, result) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(result);
                }
            });
        });
    },

    detele_order(order_id) {
        return new Promise((resolve, reject) => {
            let detale_order_sql = `DELETE FROM db_order WHERE order_id="${order_id}"`;
            db.query(detale_order_sql, (err, result) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({
                        code: 200,
                        msh: "删除订单成功",
                    });
                }
            });
        });
    },
};
